package io.renren.modules.app.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import io.renren.modules.app.service.JournalismService;
import io.renren.modules.app.service.TranslateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zheok.testing.entity.JournalismEntity;
import org.springframework.web.client.RestTemplate;
import utils.R;
import io.renren.common.utils.PageUtils;



/**
 * 新闻表
 *
 * @author ææ¢
 * @email 2863751700@qq.com
 * @date 2022-07-31 19:06:14
 */
@Slf4j
@RestController
@RequestMapping("testing/journalism")
public class JournalismController {
    @Autowired
    private JournalismService journalismService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // // //@RequiresPermissions("testing:journalism:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = journalismService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // // //@RequiresPermissions("testing:journalism:info")
    public R info(@PathVariable("id") Long id){
		JournalismEntity journalism = journalismService.getById(id);

        return R.ok().put("journalism", journalism);
    }

    @Value("${en_url}")
    String en_url;
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 保存
     */
    @RequestMapping("/save")
   // // //@RequiresPermissions("testing:journalism:save")
    public R save(@RequestBody JournalismEntity journalism) throws IOException {
		journalismService.saveinfo(journalism);
//        //TODO 发送请求添加英文数据
//        String url =en_url+"/journalism/save";
//        String transResult = TranslateUtil.getTransResult(journalism.getTitle()+"`"+journalism.getAbstractInfo(), "auto", "en");
//        String[] split = transResult.split("`");
//        journalism.setTitle(split[0]);
//        journalism.setAbstractInfo(split[1]);
//        JSONObject postData = (JSONObject) JSONObject.toJSON(journalism);
//        String body = restTemplate.postForEntity(url, postData, String.class).getBody();
//        log.info("调用英文接口的结果body:{}",body);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // // //@RequiresPermissions("testing:journalism:update")
    public R update(@RequestBody JournalismEntity journalism){
		journalismService.saveinfo(journalism);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // // //@RequiresPermissions("testing:journalism:delete")
    public R delete(@RequestBody Long[] ids){
		journalismService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
